home *** CD-ROM | disk | FTP | other *** search
/ Openstep 4.2 (Developer) / Openstep Developer 4.2.iso / NextDeveloper / Source / GNU / uucp / uucico / proti.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-20  |  47.5 KB  |  1,700 lines

  1. /* proti.c
  2.    The 'i' protocol.
  3.  
  4.    Copyright (C) 1992, 1993, 1995 Ian Lance Taylor
  5.  
  6.    This file is part of the Taylor UUCP package.
  7.  
  8.    This program is free software; you can redistribute it and/or
  9.    modify it under the terms of the GNU General Public License as
  10.    published by the Free Software Foundation; either version 2 of the
  11.    License, or (at your option) any later version.
  12.  
  13.    This program is distributed in the hope that it will be useful, but
  14.    WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.    General Public License for more details.
  17.  
  18.    You should have received a copy of the GNU General Public License
  19.    along with this program; if not, write to the Free Software
  20.    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  21.  
  22.    The author of the program may be contacted at ian@airs.com or
  23.    c/o Cygnus Support, 48 Grove Street, Somerville, MA 02144.
  24.    */
  25.  
  26. #include "uucp.h"
  27.  
  28. #if USE_RCS_ID
  29. const char proti_rcsid[] = "$Id: proti.c,v 1.33 1995/06/21 19:15:28 ian Rel $";
  30. #endif
  31.  
  32. #include <ctype.h>
  33. #include <errno.h>
  34.  
  35. #include "uudefs.h"
  36. #include "uuconf.h"
  37. #include "conn.h"
  38. #include "trans.h"
  39. #include "system.h"
  40. #include "prot.h"
  41.  
  42. /* The 'i' protocol is a simple sliding window protocol, created by
  43.    me.  It is in many ways similar to the 'g' protocol.  Several ideas
  44.    are also taken from the paper ``A High-Throughput Message Transport
  45.    System'' by P. Lauder.  I don't know where the paper was published,
  46.    but the author's e-mail address is piers@cs.su.oz.au.  However, I
  47.    haven't adopted his main idea, which is to dispense with windows
  48.    entirely.  This is because some links still do require flow control
  49.    and, more importantly, because I want to have a limit to the amount
  50.    of data I must be able to resend upon request.  To reduce the costs
  51.    of window acknowledgements, I use a large window and only require
  52.    an ack at the halfway point.
  53.  
  54.    Each packet starts with a header containing the following
  55.    information:
  56.  
  57.    Intro byte           8 bits          byte 1
  58.    Packet number        5 bits          byte 2
  59.    Local channel        3 bits
  60.    Packet ack           5 bits          byte 3
  61.    Remote channel       3 bits
  62.    Packet type          3 bits          bytes 4-5
  63.    Direction            1 bit
  64.    Data length         12 bits
  65.    Header check         8 bits          byte 6
  66.  
  67.    If the data length is not 0, this is followed by the data and a 32
  68.    bit CRC checksum.
  69.  
  70.    The following packet types are defined:
  71.  
  72.    SYNC  Initialize the connection
  73.    DATA  Data packet
  74.    ACK   Simple acknowledgement with no data
  75.    NAK   Negative acknowledgement; requests resend of single packet
  76.    SPOS  Set file position
  77.    CLOSE Close the connection
  78.    */
  79.  
  80. /* The offsets of the bytes in the packet header.  */
  81.  
  82. #define IHDR_INTRO (0)
  83. #define IHDR_LOCAL (1)
  84. #define IHDR_REMOTE (2)
  85. #define IHDR_CONTENTS1 (3)
  86. #define IHDR_CONTENTS2 (4)
  87. #define IHDR_CHECK (5)
  88.  
  89. /* Macros to set and extract values of IHDR_LOCAL and IHDR_REMOTE.  */
  90. #define IHDRWIN_SET(iseq, ichan) (((iseq) << 3) | (ichan))
  91. #define IHDRWIN_GETSEQ(ival) (((ival) >> 3) & 0x1f)
  92. #define IHDRWIN_GETCHAN(ival) ((ival) & 0x07)
  93.  
  94. /* Macros to set and extract values of IHDR_CONTENTS fields.  */
  95. #define IHDRCON_SET1(ttype, fcaller, cbytes) \
  96.   (((ttype) << 5) | ((fcaller) ? (1 << 4) : 0) | (((cbytes) >> 8) & 0x0f))
  97. #define IHDRCON_SET2(ttype, fcaller, cbytes) ((cbytes) & 0xff)
  98. #define THDRCON_GETTYPE(i1, i2) (((i1) >> 5) & 0x07)
  99. #define FHDRCON_GETCALLER(i1, i2) (((i1) & (1 << 4)) != 0)
  100. #define CHDRCON_GETBYTES(i1, i2) ((((i1) & 0x0f) << 8) | ((i2) & 0xff))
  101.  
  102. /* Macros for the IHDR_CHECK field.  */
  103. #define IHDRCHECK_VAL(zhdr) \
  104.   ((zhdr[IHDR_LOCAL] \
  105.     ^ zhdr[IHDR_REMOTE] \
  106.     ^ zhdr[IHDR_CONTENTS1] \
  107.     ^ zhdr[IHDR_CONTENTS2]) \
  108.    & 0xff)
  109.  
  110. /* Length of the packet header.  */
  111. #define CHDRLEN (6)
  112.  
  113. /* Amount of space to skip between start of packet and actual data.
  114.    This is used to make the actual data longword aligned, to encourage
  115.    good performance when copying data into the buffer.  */
  116. #define CHDRSKIPLEN (CHDRLEN + (sizeof (long) - CHDRLEN % sizeof (long)))
  117.  
  118. /* Amount of space to skip between memory buffer and header.  */
  119. #define CHDROFFSET (CHDRSKIPLEN - CHDRLEN)
  120.  
  121. /* Length of the trailing checksum.  */
  122. #define CCKSUMLEN (4)
  123.  
  124. /* Macros to set and get the checksum.  These multiply evaluate their
  125.    arguments.  */
  126. #define ICKSUM_GET(z) \
  127.   ((((((((unsigned long) ((z)[0] & 0xff)) << 8) \
  128.        | (unsigned long) ((z)[1] & 0xff)) << 8) \
  129.      | (unsigned long) ((z)[2] & 0xff)) << 8) \
  130.    | (unsigned long) ((z)[3] & 0xff))
  131. #define UCKSUM_SET(z, i) \
  132.   (void) ((z)[0] = (((i) >> 24) & 0xff), \
  133.       (z)[1] = (((i) >> 16) & 0xff), \
  134.       (z)[2] = (((i) >> 8) & 0xff), \
  135.       (z)[3] = ((i) & 0xff))
  136.  
  137. /* The header introduction character.  */
  138. #define IINTRO ('\007')
  139.  
  140. /* The packet types.  */
  141.  
  142. #define DATA (0)
  143. #define SYNC (1)
  144. #define ACK (2)
  145. #define NAK (3)
  146. #define SPOS (4)
  147. #define CLOSE (5)
  148.  
  149. /* Largest possible packet size.  */
  150. #define IMAXPACKSIZE ((1 << 12) - 1)
  151.  
  152. /* Largest possible sequence number (plus 1).  */
  153. #define IMAXSEQ 32
  154.  
  155. /* Get the next sequence number given a sequence number.  */
  156. #define INEXTSEQ(i) (((i) + 1) & (IMAXSEQ - 1))
  157.  
  158. /* Get the previous sequence number given a sequence number.  */
  159. #define IPREVSEQ(i) (((i) + IMAXSEQ - 1) & (IMAXSEQ - 1))
  160.  
  161. /* Compute i1 - i2 in sequence space (i.e., the number of packets from
  162.    i2 to i1).  */
  163. #define CSEQDIFF(i1, i2) (((i1) + IMAXSEQ - (i2)) & (IMAXSEQ - 1))
  164.  
  165. /* Largest possible channel number (plus 1).  */
  166. #define IMAXICHAN (8)
  167.  
  168. /* Default packet size to request (protocol parameter
  169.    ``packet-size'').  */
  170. #define IREQUEST_PACKSIZE (1024)
  171.  
  172. /* Default window size to request (protocol parameter ``window'').  */
  173. #define IREQUEST_WINSIZE (16)
  174.  
  175. /* Default timeout to use when sending the SYNC packet (protocol
  176.    parameter ``sync-timeout'').  */
  177. #define CSYNC_TIMEOUT (10)
  178.  
  179. /* Default number of times to retry sending the SYNC packet (protocol
  180.    parameter ``sync-retries'').  */
  181. #define CSYNC_RETRIES (6)
  182.  
  183. /* Default timeout to use when waiting for a packet (protocol
  184.    parameter ``timeout'').  */
  185. #define CTIMEOUT (10)
  186.  
  187. /* Default number of times to retry sending a packet before giving up
  188.    (protocol parameter ``retries'').  */
  189. #define CRETRIES (6)
  190.  
  191. /* Default maximum level of errors to accept before giving up
  192.    (protocol parameter ``errors'').  */
  193. #define CERRORS (100)
  194.  
  195. /* Default decay rate.  Each time we receive this many packets
  196.    succesfully, we decrement the error level by one (protocol
  197.    parameter ``error-decay'').  */
  198. #define CERROR_DECAY (10)
  199.  
  200. /* The default list of characters to avoid: XON and XOFF.  This string
  201.    is processed as an escape sequence.  This is 'j' protocol parameter
  202.    ``avoid''; it is defined in this file because the 'i' and 'j'
  203.    protocols share protocol parameters.  */
  204. #define ZAVOID "\\021\\023"
  205.  
  206. /* Local variables.  */
  207.  
  208. /* Packet size to request (protocol parameter ``packet-size'').  */
  209. static int iIrequest_packsize = IREQUEST_PACKSIZE;
  210.  
  211. /* Window size to request (protocol parameter ``window'').  */
  212. static int iIrequest_winsize = IREQUEST_WINSIZE;
  213.  
  214. /* Remote packet size (set from SYNC packet or from
  215.    iIforced_remote_packsize).  */
  216. static int iIremote_packsize;
  217.  
  218. /* Size which buffers were allocated for.  */
  219. static int iIalc_packsize;
  220.  
  221. /* Forced remote packet size, used if non-zero (protocol parameter
  222.    ``remote-packet-size'').  There is no forced remote window size
  223.    because the ACK strategy requires that both sides agree on the
  224.    window size.  */
  225. static int iIforced_remote_packsize = 0;
  226.  
  227. /* Remote window size (set from SYNC packet).  */
  228. static int iIremote_winsize;
  229.  
  230. /* Timeout to use when sending the SYNC packet (protocol
  231.    parameter ``sync-timeout'').  */
  232. int cIsync_timeout = CSYNC_TIMEOUT;
  233.  
  234. /* Number of times to retry sending the SYNC packet (protocol
  235.    parameter ``sync-retries'').  */
  236. static int cIsync_retries = CSYNC_RETRIES;
  237.  
  238. /* Timeout to use when waiting for a packet (protocol parameter
  239.    ``timeout'').  */
  240. static int cItimeout = CTIMEOUT;
  241.  
  242. /* Timeout to use when waiting for an acknowledgement to open up space
  243.    in the window.  This is computed based on the window size and the
  244.    connection speed.  */
  245. static int cIwindow_timeout = CTIMEOUT;
  246.  
  247. /* Number of times to retry sending a packet before giving up
  248.    (protocol parameter ``retries'').  */
  249. static int cIretries = CRETRIES;
  250.  
  251. /* Maximum level of errors to accept before giving up (protocol
  252.    parameter ``errors'').  */
  253. static int cIerrors = CERRORS;
  254.  
  255. /* Each time we receive this many packets succesfully, we decrement
  256.    the error level by one (protocol parameter ``error-decay'').  */
  257. static int cIerror_decay = CERROR_DECAY;
  258.  
  259. /* The number of packets we should wait to receive before sending an
  260.    ACK; this is set by default to half the window size we have
  261.    requested (protocol parameter ``ack-frequency'').  */
  262. static int cIack_frequency = 0;
  263.  
  264. /* The set of characters to avoid (protocol parameter ``avoid'').
  265.    This is actually part of the 'j' protocol; it is defined in this
  266.    file because the 'i' and 'j' protocols use the same protocol
  267.    parameters.  */
  268. const char *zJavoid_parameter = ZAVOID;
  269.  
  270. /* Routine to use when sending data.  This is a hook for the 'j'
  271.    protocol.  */
  272. static boolean (*pfIsend) P((struct sconnection *qconn, const char *zsend,
  273.                  size_t csend, boolean fdoread));
  274.  
  275. /* Routine to use to use when reading data.  This is a hook for the
  276.    'j' protocol.  */
  277. static boolean (*pfIreceive) P((struct sconnection *qconn, size_t cneed,
  278.                 size_t *pcrec, int ctimeout,
  279.                 boolean freport));
  280.  
  281. /* Next sequence number to send.  */
  282. static int iIsendseq;
  283.  
  284. /* Last sequence number received.  */
  285. static int iIrecseq;
  286.  
  287. /* Last sequence number we have acknowledged.  */
  288. static int iIlocal_ack;
  289.  
  290. /* Last sequence number remote system has acknowledged.  */
  291. static int iIremote_ack;
  292.  
  293. /* File position we are sending from.  */
  294. static long iIsendpos;
  295.  
  296. /* File position we are receiving to.  */
  297. static long iIrecpos;
  298.  
  299. /* TRUE if closing the connection.  */
  300. static boolean fIclosing;
  301.  
  302. /* Array of sent packets indexed by packet number.  */
  303. static char *azIsendbuffers[IMAXSEQ];
  304.  
  305. /* Array of received packets that we aren't ready to process yet,
  306.    indexed by packet number.  */
  307. static char *azIrecbuffers[IMAXSEQ];
  308.  
  309. /* For each packet sequence number, record whether we sent a NAK for
  310.    the packet.  */
  311. static boolean afInaked[IMAXSEQ];
  312.  
  313. /* Number of SYNC packets received (used only to detect whether one
  314.    was received).  */
  315. static int cIsyncs;
  316.  
  317. /* Number of packets sent.  */
  318. static long cIsent_packets;
  319.  
  320. /* Number of packets received.  */
  321. static long cIreceived_packets;
  322.  
  323. /* Number of packets resent.  */
  324. static long cIresent_packets;
  325.  
  326. /* Number of bad packet headers received.  */
  327. static long cIbad_hdr;
  328.  
  329. /* Number of out of order packets received.  */
  330. static long cIbad_order;
  331.  
  332. /* Number of bad checksums received.  */
  333. static long cIbad_cksum;
  334.  
  335. /* Number of packets rejected by remote system.  */
  336. static long cIremote_rejects;
  337.  
  338. /* Protocol parameter commands.  */
  339.  
  340. struct uuconf_cmdtab asIproto_params[] =
  341. {
  342.   { "packet-size", UUCONF_CMDTABTYPE_INT, (pointer) &iIrequest_packsize,
  343.       NULL },
  344.   { "window", UUCONF_CMDTABTYPE_INT, (pointer) &iIrequest_winsize, NULL },
  345.   { "remote-packet-size", UUCONF_CMDTABTYPE_INT,
  346.       (pointer) &iIforced_remote_packsize, NULL },
  347.   { "sync-timeout", UUCONF_CMDTABTYPE_INT, (pointer) &cIsync_timeout,
  348.       NULL },
  349.   { "sync-retries", UUCONF_CMDTABTYPE_INT, (pointer) &cIsync_retries,
  350.       NULL },
  351.   { "timeout", UUCONF_CMDTABTYPE_INT, (pointer) &cItimeout, NULL },
  352.   { "retries", UUCONF_CMDTABTYPE_INT, (pointer) &cIretries, NULL },
  353.   { "errors", UUCONF_CMDTABTYPE_INT, (pointer) &cIerrors, NULL },
  354.   { "error-decay", UUCONF_CMDTABTYPE_INT, (pointer) &cIerror_decay, NULL },
  355.   { "ack-frequency", UUCONF_CMDTABTYPE_INT, (pointer) &cIack_frequency, NULL },
  356.   /* The ``avoid'' protocol parameter is part of the 'j' protocol, but
  357.      it is convenient for the 'i' and 'j' protocols to share the same
  358.      protocol parameter table.  */
  359.   { "avoid", UUCONF_CMDTABTYPE_STRING, (pointer) &zJavoid_parameter, NULL },
  360.   { NULL, 0, NULL, NULL }
  361. };
  362.  
  363. /* Local functions.  */
  364.  
  365. static boolean finak P((struct sdaemon *qdaemon, int iseq));
  366. static boolean firesend P((struct sdaemon *qdaemon));
  367. static boolean fiwindow_wait P((struct sdaemon *qdaemon));
  368. static boolean fiwait_for_packet P((struct sdaemon *qdaemon,
  369.                     int ctimeout, int cretries,
  370.                     boolean fone, boolean *ftimedout));
  371. static boolean ficheck_errors P((struct sdaemon *qdaemon));
  372. static boolean fiprocess_data P((struct sdaemon *qdaemon,
  373.                  boolean *pfexit, boolean *pffound,
  374.                  size_t *pcneed));
  375. static boolean fiprocess_packet P((struct sdaemon *qdaemon,
  376.                    const char *zhdr,
  377.                    const char *zfirst, int cfirst,
  378.                    const char *zsecond, int csecond,
  379.                    boolean *pfexit));
  380.  
  381. /* The 'i' protocol start routine.  The work is done in a routine
  382.    which is also called by the 'j' protocol start routine.  */
  383.  
  384. boolean
  385. fistart (qdaemon, pzlog)
  386.      struct sdaemon *qdaemon;
  387.      char **pzlog;
  388. {
  389.   return fijstart (qdaemon, pzlog, IMAXPACKSIZE, fsend_data, freceive_data);
  390. }
  391.  
  392. /* Start the protocol.  This routine is called by both the 'i' and 'j'
  393.    protocol start routines.  We keep transmitting a SYNC packet
  394.    containing the window and packet size we would like to receive
  395.    until we receive a SYNC packet from the remote system.  The first
  396.    two bytes of the data contents of a SYNC packet are the maximum
  397.    packet size we want to receive (high byte, low byte), and the next
  398.    byte is the maximum window size we want to use.  */
  399.  
  400. boolean
  401. fijstart (qdaemon, pzlog, imaxpacksize, pfsend, pfreceive)
  402.      struct sdaemon *qdaemon;
  403.      char **pzlog;
  404.      int imaxpacksize;
  405.      boolean (*pfsend) P((struct sconnection *qconn, const char *zsend,
  406.               size_t csend, boolean fdoread));
  407.      boolean (*pfreceive) P((struct sconnection *qconn, size_t cneed,
  408.                  size_t *pcrec, int ctimeout, boolean freport));
  409. {
  410.   char ab[CHDRLEN + 4 + CCKSUMLEN];
  411.   unsigned long icksum;
  412.   int ctries;
  413.   int csyncs;
  414.   long ibaud;
  415.  
  416.   *pzlog = NULL;
  417.  
  418.   pfIsend = pfsend;
  419.   pfIreceive = pfreceive;
  420.  
  421.   if (iIforced_remote_packsize <= 0
  422.       || iIforced_remote_packsize > imaxpacksize)
  423.     iIforced_remote_packsize = 0;
  424.   else
  425.     iIremote_packsize = iIforced_remote_packsize;
  426.   iIalc_packsize = 0;
  427.  
  428.   iIsendseq = 1;
  429.   iIrecseq = 0;
  430.   iIlocal_ack = 0;
  431.   iIremote_ack = 0;
  432.   iIsendpos = 0;
  433.   iIrecpos = 0;
  434.   fIclosing = FALSE;
  435.  
  436.   cIsent_packets = 0;
  437.   cIreceived_packets = 0;
  438.   cIresent_packets = 0;
  439.   cIbad_hdr = 0;
  440.   cIbad_order = 0;
  441.   cIbad_cksum = 0;
  442.   cIremote_rejects = 0;
  443.  
  444.   if (iIrequest_packsize < 0 || iIrequest_packsize > imaxpacksize)
  445.     {
  446.       ulog (LOG_ERROR, "Illegal protocol '%c' packet size; using %d",
  447.         qdaemon->qproto->bname, imaxpacksize);
  448.       iIrequest_packsize = imaxpacksize;
  449.     }
  450.  
  451.   /* The maximum permissible window size is 16.  Otherwise the
  452.      protocol can get confused because a duplicated packet may arrive
  453.      out of order.  If the window size is large in such a case, the
  454.      duplicate packet may be treated as a packet in the upcoming
  455.      window, causing the protocol to assume that all intermediate
  456.      packets have been lost, leading to immense confusion.  */
  457.   if (iIrequest_winsize < 0 || iIrequest_winsize > IMAXSEQ / 2)
  458.     {
  459.       ulog (LOG_ERROR, "Illegal protocol '%c' window size; using %d",
  460.         qdaemon->qproto->bname, IREQUEST_WINSIZE);
  461.       iIrequest_winsize = IREQUEST_WINSIZE;
  462.     }
  463.  
  464.   /* The default for the ACK frequency is half the window size.  */
  465.   if (cIack_frequency <= 0 || cIack_frequency >= iIrequest_winsize)
  466.     cIack_frequency = iIrequest_winsize / 2;
  467.  
  468.   ab[IHDR_INTRO] = IINTRO;
  469.   ab[IHDR_LOCAL] = ab[IHDR_REMOTE] = IHDRWIN_SET (0, 0);
  470.   ab[IHDR_CONTENTS1] = IHDRCON_SET1 (SYNC, qdaemon->fcaller, 4);
  471.   ab[IHDR_CONTENTS2] = IHDRCON_SET2 (SYNC, qdaemon->fcaller, 4);
  472.   ab[IHDR_CHECK] = IHDRCHECK_VAL (ab);
  473.   ab[CHDRLEN + 0] = (iIrequest_packsize >> 8) & 0xff;
  474.   ab[CHDRLEN + 1] = iIrequest_packsize & 0xff;
  475.   ab[CHDRLEN + 2] = iIrequest_winsize;
  476.   ab[CHDRLEN + 3] = qdaemon->cchans;
  477.   icksum = icrc (ab + CHDRLEN, 4, ICRCINIT);
  478.   UCKSUM_SET (ab + CHDRLEN + 4, icksum);
  479.  
  480.   /* The static cIsyncs is incremented each time a SYNC packet is
  481.      received.  */
  482.   csyncs = cIsyncs;
  483.   ctries = 0;
  484.  
  485.   while (TRUE)
  486.     {
  487.       boolean ftimedout;
  488.  
  489.       DEBUG_MESSAGE3 (DEBUG_PROTO,
  490.               "fistart: Sending SYNC packsize %d winsize %d channels %d",
  491.               iIrequest_packsize, iIrequest_winsize, qdaemon->cchans);
  492.  
  493.       if (! (*pfIsend) (qdaemon->qconn, ab, CHDRLEN + 4 + CCKSUMLEN,
  494.             TRUE))
  495.     return FALSE;
  496.  
  497.       if (fiwait_for_packet (qdaemon, cIsync_timeout, 0, FALSE,
  498.                  &ftimedout))
  499.     {
  500.       if (csyncs != cIsyncs)
  501.         break;
  502.     }
  503.       else
  504.     {
  505.       if (! ftimedout)
  506.         return FALSE;
  507.  
  508.       ++ctries;
  509.       if (ctries > cIsync_retries)
  510.         {
  511.           ulog (LOG_ERROR, "Protocol startup failed");
  512.           return FALSE;
  513.         }
  514.     }
  515.     }
  516.  
  517.   /* Calculate the window timeout.  */
  518.   ibaud = iconn_baud (qdaemon->qconn);
  519.   if (ibaud == 0)
  520.     cIwindow_timeout = cItimeout;
  521.   else
  522.     {
  523.       /* We expect to receive an ACK about halfway through each
  524.      window.  In principle, an entire window might be sitting in a
  525.      modem buffer while we are waiting for an ACK.  Therefore, the
  526.      minimum time we should wait for an ACK is
  527.        (1/2 window size) * (seconds / byte) + (roundtrip time) ==
  528.        (1/2 window size) * (1 / (baud / 10)) + (roundtrip time) ==
  529.        (1/2 window size) * (10 / baud) + (roundtrip time) ==
  530.        (5 * (window size)) / baud + (roundtrip time)
  531.  
  532.      The window size is iIremote_packsize * iIremote_winsize.  For
  533.      typical settings of packsize == 1024, winsize == 16, baud ==
  534.      9600, this equation works out to
  535.        (5 * 1024 * 16) / 9600 == 8 seconds
  536.      We then take cItimeout as the round trip time, which gives us
  537.      some flexibility.  We get more flexibility because it is
  538.      quite likely that by the time we have finished sending out
  539.      the last packet in a window, the first one has already been
  540.      received by the remote system.  */
  541.       cIwindow_timeout = ((5 * iIremote_packsize * iIremote_winsize) / ibaud
  542.               + cItimeout);
  543.     }
  544.  
  545.   /* If we are the callee, bump both timeouts by one, to make it less
  546.      likely that both systems will timeout simultaneously.  */
  547.   if (! qdaemon->fcaller)
  548.     {
  549.       ++cItimeout;
  550.       ++cIwindow_timeout;
  551.     }
  552.  
  553.   /* We got a SYNC packet; set up packet buffers to use.  */
  554.   if (iIremote_packsize > imaxpacksize)
  555.     iIremote_packsize = imaxpacksize;
  556.   do
  557.     {
  558.       int iseq;
  559.  
  560.       for (iseq = 0; iseq < IMAXSEQ; iseq++)
  561.     {
  562.       azIrecbuffers[iseq] = NULL;
  563.       afInaked[iseq] = FALSE;
  564.       azIsendbuffers[iseq] = (char *) malloc (iIremote_packsize
  565.                           + CHDRSKIPLEN
  566.                           + CCKSUMLEN);
  567.       if (azIsendbuffers[iseq] == NULL)
  568.         {
  569.           int ifree;
  570.  
  571.           for (ifree = 0; ifree < iseq; ifree++)
  572.         free ((pointer) azIsendbuffers[ifree]);
  573.           break;
  574.         }
  575.     }
  576.  
  577.       if (iseq >= IMAXSEQ)
  578.     {
  579.       *pzlog =
  580.         zbufalc (sizeof "protocol '' sending packet/window / receiving /"
  581.              + 64);
  582.       sprintf (*pzlog,
  583.            "protocol '%c' sending packet/window %d/%d receiving %d/%d",
  584.            qdaemon->qproto->bname, (int) iIremote_packsize,
  585.            (int) iIremote_winsize, (int) iIrequest_packsize,
  586.            (int) iIrequest_winsize);
  587.  
  588.       iIalc_packsize = iIremote_packsize;
  589.  
  590.       return TRUE;
  591.     }
  592.  
  593.       iIremote_packsize >>= 1;
  594.     }
  595.   while (iIremote_packsize > 200);
  596.  
  597.   ulog (LOG_ERROR,
  598.     "'%c' protocol startup failed; insufficient memory for packets",
  599.     qdaemon->qproto->bname);
  600.  
  601.   return FALSE;
  602. }
  603.  
  604. /* Shut down the protocol.  We can be fairly informal about this,
  605.    since we know that the upper level protocol has already exchanged
  606.    hangup messages.  If we didn't know that, we would have to make
  607.    sure that all packets before the CLOSE had been received.  */
  608.  
  609. boolean
  610. fishutdown (qdaemon)
  611.      struct sdaemon *qdaemon;
  612. {
  613.   char *z;
  614.   size_t clen;
  615.  
  616.   fIclosing = TRUE;
  617.  
  618.   z = zigetspace (qdaemon, &clen) - CHDRLEN;
  619.  
  620.   z[IHDR_INTRO] = IINTRO;
  621.   z[IHDR_LOCAL] = IHDRWIN_SET (iIsendseq, 0);
  622.   z[IHDR_REMOTE] = IHDRWIN_SET (iIrecseq, 0);
  623.   iIlocal_ack = iIrecseq;
  624.   z[IHDR_CONTENTS1] = IHDRCON_SET1 (CLOSE, qdaemon->fcaller, 0);
  625.   z[IHDR_CONTENTS2] = IHDRCON_SET2 (CLOSE, qdaemon->fcaller, 0);
  626.   z[IHDR_CHECK] = IHDRCHECK_VAL (z);
  627.  
  628.   DEBUG_MESSAGE0 (DEBUG_PROTO, "fishutdown: Sending CLOSE");
  629.  
  630.   if (! (*pfIsend) (qdaemon->qconn, z, CHDRLEN, FALSE))
  631.     return FALSE;
  632.  
  633.   ulog (LOG_NORMAL,
  634.     "Protocol '%c' packets: sent %ld, resent %ld, received %ld",
  635.     qdaemon->qproto->bname, cIsent_packets, cIresent_packets,
  636.     cIreceived_packets);
  637.   if (cIbad_hdr != 0
  638.       || cIbad_cksum != 0
  639.       || cIbad_order != 0
  640.       || cIremote_rejects != 0)
  641.     ulog (LOG_NORMAL,
  642.       "Errors: header %ld, checksum %ld, order %ld, remote rejects %ld",
  643.       cIbad_hdr, cIbad_cksum, cIbad_order, cIremote_rejects);
  644.  
  645.   /* Reset the protocol parameters to their default values.  */
  646.   iIrequest_packsize = IREQUEST_PACKSIZE;
  647.   iIrequest_winsize = IREQUEST_WINSIZE;
  648.   iIforced_remote_packsize = 0;
  649.   cIsync_timeout = CSYNC_TIMEOUT;
  650.   cIsync_retries = CSYNC_RETRIES;
  651.   cItimeout = CTIMEOUT;
  652.   cIwindow_timeout = CTIMEOUT;
  653.   cIretries = CRETRIES;
  654.   cIerrors = CERRORS;
  655.   cIerror_decay = CERROR_DECAY;
  656.   cIack_frequency = 0;
  657.   zJavoid_parameter = ZAVOID;
  658.  
  659.   return TRUE;
  660. }
  661.  
  662. /* Send a command string.  These are just sent as normal packets,
  663.    ending in a packet containing a null byte.  */
  664.  
  665. boolean
  666. fisendcmd (qdaemon, z, ilocal, iremote)
  667.      struct sdaemon *qdaemon;
  668.      const char *z;
  669.      int ilocal;
  670.      int iremote;
  671. {
  672.   size_t clen;
  673.  
  674.   DEBUG_MESSAGE1 (DEBUG_UUCP_PROTO, "fisendcmd: Sending command \"%s\"", z);
  675.  
  676.   clen = strlen (z);
  677.  
  678.   while (TRUE)
  679.     {
  680.       char *zpacket;
  681.       size_t csize;
  682.  
  683.       zpacket = zigetspace (qdaemon, &csize);
  684.  
  685.       if (clen < csize)
  686.     {
  687.       memcpy (zpacket, z, clen + 1);
  688.       return fisenddata (qdaemon, zpacket, clen + 1, ilocal, iremote,
  689.                  (long) -1);
  690.     }
  691.  
  692.       memcpy (zpacket, z, csize);
  693.       z += csize;
  694.       clen -= csize;
  695.  
  696.       if (! fisenddata (qdaemon, zpacket, csize, ilocal, iremote, (long) -1))
  697.     return FALSE;
  698.     }
  699.   /*NOTREACHED*/
  700. }
  701.  
  702. /* Send a NAK.  */
  703.  
  704. static boolean
  705. finak (qdaemon, iseq)
  706.      struct sdaemon *qdaemon;
  707.      int iseq;
  708. {
  709.   char abnak[CHDRLEN];
  710.  
  711.   abnak[IHDR_INTRO] = IINTRO;
  712.   abnak[IHDR_LOCAL] = IHDRWIN_SET (iseq, 0);
  713.   abnak[IHDR_REMOTE] = IHDRWIN_SET (iIrecseq, 0);
  714.   iIlocal_ack = iIrecseq;
  715.   abnak[IHDR_CONTENTS1] = IHDRCON_SET1 (NAK, qdaemon->fcaller, 0);
  716.   abnak[IHDR_CONTENTS2] = IHDRCON_SET2 (NAK, qdaemon->fcaller, 0);
  717.   abnak[IHDR_CHECK] = IHDRCHECK_VAL (abnak);
  718.  
  719.   afInaked[iseq] = TRUE;
  720.  
  721.   DEBUG_MESSAGE1 (DEBUG_PROTO | DEBUG_ABNORMAL,
  722.           "finak: Sending NAK %d", iseq);
  723.  
  724.   return (*pfIsend) (qdaemon->qconn, abnak, CHDRLEN, TRUE);
  725. }
  726.  
  727. /* Resend the latest packet the remote has not acknowledged.  */
  728.  
  729. static boolean
  730. firesend (qdaemon)
  731.      struct sdaemon *qdaemon;
  732. {
  733.   int iseq;
  734.   char *zhdr;
  735.   size_t clen;
  736.  
  737.   iseq = INEXTSEQ (iIremote_ack);
  738.   if (iseq == iIsendseq)
  739.     {
  740.       /* Everything has been ACKed and there is nothing to resend.  */
  741.       return TRUE;
  742.     }
  743.  
  744.   DEBUG_MESSAGE1 (DEBUG_PROTO | DEBUG_ABNORMAL,
  745.           "firesend: Resending packet %d", iseq);
  746.  
  747.   /* Update the received sequence number.  */
  748.   zhdr = azIsendbuffers[iseq] + CHDROFFSET;
  749.   if (IHDRWIN_GETSEQ (zhdr[IHDR_REMOTE]) != iIrecseq)
  750.     {
  751.       int iremote;
  752.  
  753.       iremote = IHDRWIN_GETCHAN (zhdr[IHDR_REMOTE]);
  754.       zhdr[IHDR_REMOTE] = IHDRWIN_SET (iIrecseq, iremote);
  755.       zhdr[IHDR_CHECK] = IHDRCHECK_VAL (zhdr);
  756.       iIlocal_ack = iIrecseq;
  757.     }
  758.  
  759.   ++cIresent_packets;
  760.  
  761.   clen = CHDRCON_GETBYTES (zhdr[IHDR_CONTENTS1],
  762.                zhdr[IHDR_CONTENTS2]);
  763.  
  764.   return (*pfIsend) (qdaemon->qconn, zhdr,
  765.              CHDRLEN + clen + (clen > 0 ? CCKSUMLEN : 0),
  766.              TRUE);
  767. }
  768.  
  769. /* Wait until there is an opening in the receive window of the remote
  770.    system.  */
  771.  
  772. static boolean
  773. fiwindow_wait (qdaemon)
  774.      struct sdaemon *qdaemon;
  775. {
  776.   /* iIsendseq is the sequence number we are sending, and iIremote_ack
  777.      is the last sequence number acknowledged by the remote. */
  778.   while (CSEQDIFF (iIsendseq, iIremote_ack) > iIremote_winsize)
  779.     {
  780.       /* If a NAK is lost, it is possible for the other side to be
  781.      sending a stream of packets while we are waiting for an ACK.
  782.      This should be caught in fiprocess_data; if it is about to
  783.      send an ACK, but it has an unacknowledged packet to send, it
  784.      sends the entire packet.  Hopefully that will trigger an ACK
  785.      or a NAK and get us going again.  */
  786.       DEBUG_MESSAGE0 (DEBUG_PROTO, "fiwindow_wait: Waiting for ACK");
  787.       if (! fiwait_for_packet (qdaemon, cIwindow_timeout, cIretries,
  788.                    TRUE, (boolean *) NULL))
  789.     return FALSE;
  790.     }
  791.  
  792.   return TRUE;
  793. }
  794.  
  795. /* Get buffer space to use for packet data.  We return a pointer to
  796.    the space to be used for the actual data, leaving room for the
  797.    header.  */
  798.  
  799. /*ARGSUSED*/
  800. char *
  801. zigetspace (qdaemon, pclen)
  802.      struct sdaemon *qdaemon;
  803.      size_t *pclen;
  804. {
  805.   *pclen = iIremote_packsize;
  806.   return azIsendbuffers[iIsendseq] + CHDRSKIPLEN;
  807. }
  808.  
  809. /* Send a data packet.  The zdata argument will always point to value
  810.    returned by zigetspace, so we know that we have room before it for
  811.    the header information.  */
  812.  
  813. boolean
  814. fisenddata (qdaemon, zdata, cdata, ilocal, iremote, ipos)
  815.      struct sdaemon *qdaemon;
  816.      char *zdata;
  817.      size_t cdata;
  818.      int ilocal;
  819.      int iremote;
  820.      long ipos;
  821. {
  822.   char *zhdr;
  823.   unsigned long icksum;
  824.   boolean fret;
  825.  
  826. #if DEBUG > 0
  827.   if (ilocal < 0 || ilocal >= IMAXICHAN
  828.       || iremote < 0 || iremote >= IMAXICHAN)
  829.     ulog (LOG_FATAL, "fisenddata: ilocal %d iremote %d", ilocal, iremote);
  830. #endif
  831.  
  832.   /* If we are changing the file position, we must send an SPOS
  833.      packet.  */
  834.   if (ipos != iIsendpos && ipos != (long) -1)
  835.     {
  836.       int inext;
  837.       char *zspos;
  838.  
  839.       /* We need to get a buffer to hold the SPOS packet, and it needs
  840.      to be next sequence number.  However, the data we have been
  841.      given is currently in the next sequence number buffer.  So we
  842.      shuffle the buffers around.  */
  843.       inext = INEXTSEQ (iIsendseq);
  844.       zspos = azIsendbuffers[inext];
  845.       azIsendbuffers[inext] = zdata - CHDRSKIPLEN;
  846.       azIsendbuffers[iIsendseq] = zspos;
  847.       zspos += CHDROFFSET;
  848.  
  849.       zspos[IHDR_INTRO] = IINTRO;
  850.       zspos[IHDR_LOCAL] = IHDRWIN_SET (iIsendseq, 0);
  851.       zspos[IHDR_REMOTE] = IHDRWIN_SET (iIrecseq, 0);
  852.       iIlocal_ack = iIrecseq;
  853.       zspos[IHDR_CONTENTS1] = IHDRCON_SET1 (SPOS, qdaemon->fcaller,
  854.                         CCKSUMLEN);
  855.       zspos[IHDR_CONTENTS2] = IHDRCON_SET2 (SPOS, qdaemon->fcaller,
  856.                         CCKSUMLEN);
  857.       zspos[IHDR_CHECK] = IHDRCHECK_VAL (zspos);
  858.       UCKSUM_SET (zspos + CHDRLEN, (unsigned long) ipos);
  859.       icksum = icrc (zspos + CHDRLEN, CCKSUMLEN, ICRCINIT);
  860.       UCKSUM_SET (zspos + CHDRLEN + CCKSUMLEN, icksum);
  861.  
  862.       /* Wait for an opening in the window.  */
  863.       if (iIremote_winsize > 0
  864.       && CSEQDIFF (iIsendseq, iIremote_ack) > iIremote_winsize)
  865.     {
  866.       if (! fiwindow_wait (qdaemon))
  867.         return FALSE;
  868.     }
  869.  
  870.       DEBUG_MESSAGE1 (DEBUG_PROTO, "fisenddata: Sending SPOS %ld",
  871.               ipos);
  872.  
  873.       if (! (*pfIsend) (qdaemon->qconn, zspos,
  874.             CHDRLEN + CCKSUMLEN + CCKSUMLEN, TRUE))
  875.     return FALSE;
  876.  
  877.       iIsendseq = INEXTSEQ (iIsendseq);
  878.       iIsendpos = ipos;
  879.     }
  880.  
  881.   zhdr = zdata - CHDRLEN;
  882.   zhdr[IHDR_INTRO] = IINTRO;
  883.   zhdr[IHDR_LOCAL] = IHDRWIN_SET (iIsendseq, ilocal);
  884.   zhdr[IHDR_CONTENTS1] = IHDRCON_SET1 (DATA, qdaemon->fcaller, cdata);
  885.   zhdr[IHDR_CONTENTS2] = IHDRCON_SET2 (DATA, qdaemon->fcaller, cdata);
  886.  
  887.   /* Compute and set the checksum.  */
  888.   if (cdata > 0)
  889.     {
  890.       icksum = icrc (zdata, cdata, ICRCINIT);
  891.       UCKSUM_SET (zdata + cdata, icksum);
  892.     }
  893.  
  894.   /* Wait until there is an opening in the window (we hope to not have
  895.      to wait here at all, actually; ideally the window should be large
  896.      enough to avoid a wait).  */
  897.   if (iIremote_winsize > 0
  898.       && CSEQDIFF (iIsendseq, iIremote_ack) > iIremote_winsize)
  899.     {
  900.       if (! fiwindow_wait (qdaemon))
  901.     return FALSE;
  902.     }
  903.  
  904.   /* We only fill in IHDR_REMOTE now, since only now do know the
  905.      correct value of iIrecseq.  */
  906.   zhdr[IHDR_REMOTE] = IHDRWIN_SET (iIrecseq, iremote);
  907.   iIlocal_ack = iIrecseq;
  908.   zhdr[IHDR_CHECK] = IHDRCHECK_VAL (zhdr);
  909.  
  910.   DEBUG_MESSAGE4 (DEBUG_PROTO,
  911.           "fisenddata: Sending packet %d size %d local %d remote %d",
  912.           iIsendseq, (int) cdata, ilocal, iremote);          
  913.  
  914.   iIsendseq = INEXTSEQ (iIsendseq);
  915.   ++cIsent_packets;
  916.  
  917.   fret = (*pfIsend) (qdaemon->qconn, zhdr,
  918.              cdata + CHDRLEN + (cdata > 0 ? CCKSUMLEN : 0),
  919.              TRUE);
  920.  
  921.   iIsendpos += cdata;
  922.  
  923.   if (fret && iPrecstart != iPrecend)
  924.     {
  925.       boolean fexit;
  926.  
  927.       fret = fiprocess_data (qdaemon, &fexit, (boolean *) NULL,
  928.                  (size_t *) NULL);
  929.     }
  930.  
  931.   return fret;
  932. }
  933.  
  934. /* Wait for data to come in.  */
  935.  
  936. boolean
  937. fiwait (qdaemon)
  938.      struct sdaemon *qdaemon;
  939. {
  940.   return fiwait_for_packet (qdaemon, cItimeout, cIretries,
  941.                 FALSE, (boolean *) NULL);
  942. }
  943.  
  944. /* Wait for a packet.  Either there is no data to send, or the remote
  945.    window is full.  */
  946.  
  947. static boolean
  948. fiwait_for_packet (qdaemon, ctimeout, cretries, fone, pftimedout)
  949.      struct sdaemon *qdaemon;
  950.      int ctimeout;
  951.      int cretries;
  952.      boolean fone;
  953.      boolean *pftimedout;
  954. {
  955.   int cshort;
  956.   int ctimeouts;
  957.  
  958.   if (pftimedout != NULL)
  959.     *pftimedout = FALSE;
  960.  
  961.   cshort = 0;
  962.   ctimeouts = 0;
  963.  
  964.   while (TRUE)
  965.     {
  966.       boolean fexit, ffound;
  967.       size_t cneed;
  968.       size_t crec;
  969.  
  970.       if (! fiprocess_data (qdaemon, &fexit, &ffound, &cneed))
  971.     return FALSE;
  972.  
  973.       if (fexit || (fone && ffound))
  974.     return TRUE;
  975.  
  976.       if (cneed == 0)
  977.     continue;
  978.  
  979.       DEBUG_MESSAGE1 (DEBUG_PROTO, "fiwait_for_packet: Need %d bytes",
  980.               (int) cneed);
  981.  
  982.       if (! (*pfIreceive) (qdaemon->qconn, cneed, &crec, ctimeout, TRUE))
  983.     return FALSE;
  984.  
  985.       if (crec != 0)
  986.     {
  987.       /* If we didn't get enough data twice in a row, we may have
  988.          dropped some data and be waiting for the end of a large
  989.          packet.  Incrementing iPrecstart will force
  990.          fiprocess_data to skip the current packet and try to find
  991.          the next one.  */
  992.       if (crec >= cneed)
  993.         cshort = 0;
  994.       else
  995.         {
  996.           ++cshort;
  997.           if (cshort > 1)
  998.         {
  999.           iPrecstart = (iPrecstart + 1) % CRECBUFLEN;
  1000.           cshort = 0;
  1001.         }
  1002.         }
  1003.     }
  1004.       else
  1005.     {
  1006.       int i;
  1007.  
  1008.       /* We timed out on the read.  */
  1009.       ++ctimeouts;
  1010.       if (ctimeouts > cretries)
  1011.         {
  1012.           if (cretries > 0)
  1013.         ulog (LOG_ERROR, "Timed out waiting for packet");
  1014.           if (pftimedout != NULL)
  1015.         *pftimedout = TRUE;
  1016.           return FALSE;
  1017.         }
  1018.  
  1019.       /* Clear out the list of packets we have sent NAKs for.  We
  1020.          should have seen some sort of response by now.  */
  1021.       for (i = 0; i < IMAXSEQ; i++)
  1022.         afInaked[i] = FALSE;
  1023.  
  1024.       /* Send a NAK for the packet we want, and, if we have an
  1025.          unacknowledged packet, send it again.  */
  1026.       if (! finak (qdaemon, INEXTSEQ (iIrecseq))
  1027.           || ! firesend (qdaemon))
  1028.         return FALSE;
  1029.     }
  1030.     }
  1031.   /*NOTREACHED*/
  1032. }
  1033.  
  1034. /* Make sure we haven't overflowed the permissible error level.  */
  1035.  
  1036. static boolean
  1037. ficheck_errors (qdaemon)
  1038.      struct sdaemon *qdaemon;
  1039. {
  1040.   if (cIerrors < 0)
  1041.     return TRUE;
  1042.  
  1043.   if (((cIbad_order + cIbad_hdr + cIbad_cksum + cIremote_rejects)
  1044.        - (cIreceived_packets / cIerror_decay))
  1045.       > cIerrors)
  1046.     {
  1047.       /* Try shrinking the packet size.  */
  1048.       if (iIrequest_packsize > 400)
  1049.     {
  1050.       char absync[CHDRLEN + 3 + CCKSUMLEN];
  1051.       unsigned long icksum;
  1052.  
  1053.       /* Don't bother sending the number of channels in this
  1054.          packet.  */
  1055.       iIrequest_packsize /= 2;
  1056.       absync[IHDR_INTRO] = IINTRO;
  1057.       absync[IHDR_LOCAL] = IHDRWIN_SET (0, 0);
  1058.       absync[IHDR_REMOTE] = IHDRWIN_SET (iIrecseq, 0);
  1059.       iIlocal_ack = iIrecseq;
  1060.       absync[IHDR_CONTENTS1] = IHDRCON_SET1 (SYNC, qdaemon->fcaller, 3);
  1061.       absync[IHDR_CONTENTS2] = IHDRCON_SET2 (SYNC, qdaemon->fcaller, 3);
  1062.       absync[IHDR_CHECK] = IHDRCHECK_VAL (absync);
  1063.       absync[CHDRLEN + 0] = (iIrequest_packsize >> 8) & 0xff;
  1064.       absync[CHDRLEN + 1] = iIrequest_packsize & 0xff;
  1065.       absync[CHDRLEN + 2] = iIrequest_winsize;
  1066.       icksum = icrc (absync + CHDRLEN, 3, ICRCINIT);
  1067.       UCKSUM_SET (absync + CHDRLEN + 3, icksum);
  1068.  
  1069.       cIerrors *= 2;
  1070.  
  1071.       DEBUG_MESSAGE2 (DEBUG_PROTO | DEBUG_ABNORMAL,
  1072.               "ficheck_errors: Sending SYNC packsize %d winsize %d",
  1073.               iIrequest_packsize, iIrequest_winsize);
  1074.  
  1075.       return (*pfIsend) (qdaemon->qconn, absync,
  1076.                  CHDRLEN + 3 + CCKSUMLEN, TRUE);
  1077.     }
  1078.  
  1079.       ulog (LOG_ERROR, "Too many '%c' protocol errors",
  1080.         qdaemon->qproto->bname);
  1081.       return FALSE;
  1082.     }
  1083.  
  1084.   return TRUE;
  1085. }
  1086.  
  1087. /* Process data waiting in the receive buffer, passing to the
  1088.    fgot_data function.  */
  1089.  
  1090. static boolean
  1091. fiprocess_data (qdaemon, pfexit, pffound, pcneed)
  1092.      struct sdaemon *qdaemon;
  1093.      boolean *pfexit;
  1094.      boolean *pffound;
  1095.      size_t *pcneed;
  1096. {
  1097.   boolean fbadhdr;
  1098.  
  1099.   if (pfexit != NULL)
  1100.     *pfexit = FALSE;
  1101.   if (pffound != NULL)
  1102.     *pffound = FALSE;
  1103.  
  1104.   fbadhdr = FALSE;
  1105.  
  1106.   while (iPrecstart != iPrecend)
  1107.     {
  1108.       char ab[CHDRLEN];
  1109.       int cfirst, csecond;
  1110.       char *zfirst, *zsecond;
  1111.       int i;
  1112.       int iget;
  1113.       int ttype;
  1114.       int iseq;
  1115.       int csize;
  1116.       int iack;
  1117.  
  1118.       /* If we're closing the connection, ignore any data remaining in
  1119.      the input buffer.  */
  1120.       if (fIclosing)
  1121.     {
  1122.       if (pfexit != NULL)
  1123.         *pfexit = TRUE;
  1124.       if (pcneed != NULL)
  1125.         *pcneed = 0;
  1126.       return TRUE;
  1127.     }
  1128.  
  1129.       /* Look for the IINTRO character.  */
  1130.       if (abPrecbuf[iPrecstart] != IINTRO)
  1131.     {
  1132.       char *zintro;
  1133.       int cintro;
  1134.  
  1135.       cintro = iPrecend - iPrecstart;
  1136.       if (cintro < 0)
  1137.         cintro = CRECBUFLEN - iPrecstart;
  1138.  
  1139.       zintro = memchr (abPrecbuf + iPrecstart, IINTRO, (size_t) cintro);
  1140.  
  1141.       if (zintro == NULL)
  1142.         {
  1143.           iPrecstart = (iPrecstart + cintro) % CRECBUFLEN;
  1144.           continue;
  1145.         }
  1146.  
  1147.       /* We don't need % CRECBUFLEN here because zintro - (abPrecbuf
  1148.          + iPrecstart) < cintro <= CRECBUFLEN - iPrecstart.  */
  1149.       iPrecstart += zintro - (abPrecbuf + iPrecstart);
  1150.     }
  1151.  
  1152.       /* Get the header into ab.  */
  1153.       for (i = 0, iget = iPrecstart;
  1154.        i < CHDRLEN && iget != iPrecend;
  1155.        i++, iget = (iget + 1) % CRECBUFLEN)
  1156.     ab[i] = abPrecbuf[iget];
  1157.  
  1158.       if (i < CHDRLEN)
  1159.     {
  1160.       if (pcneed != NULL)
  1161.         *pcneed = CHDRLEN - i;
  1162.       return TRUE;
  1163.     }
  1164.  
  1165.       if ((ab[IHDR_CHECK] & 0xff) != IHDRCHECK_VAL (ab)
  1166.       || (FHDRCON_GETCALLER (ab[IHDR_CONTENTS1], ab[IHDR_CONTENTS2])
  1167.           ? qdaemon->fcaller : ! qdaemon->fcaller))
  1168.     {
  1169.       /* We only report a single bad header message per call, to
  1170.          avoid generating many errors if we get many INTRO bytes
  1171.          in a row.  */
  1172.       if (! fbadhdr)
  1173.         {
  1174.           DEBUG_MESSAGE0 (DEBUG_PROTO | DEBUG_ABNORMAL,
  1175.                   "fiprocess_data: Bad header");
  1176.  
  1177.           ++cIbad_hdr;
  1178.           if (! ficheck_errors (qdaemon))
  1179.         return FALSE;
  1180.  
  1181.           fbadhdr = TRUE;
  1182.         }
  1183.  
  1184.       iPrecstart = (iPrecstart + 1) % CRECBUFLEN;
  1185.       continue;
  1186.     }
  1187.  
  1188.       zfirst = zsecond = NULL;
  1189.       cfirst = csecond = 0;
  1190.  
  1191.       ttype = THDRCON_GETTYPE (ab[IHDR_CONTENTS1], ab[IHDR_CONTENTS2]);
  1192.       if (ttype == DATA || ttype == SPOS || ttype == CLOSE)
  1193.     iseq = IHDRWIN_GETSEQ (ab[IHDR_LOCAL]);
  1194.       else
  1195.     iseq = -1;
  1196.       csize = CHDRCON_GETBYTES (ab[IHDR_CONTENTS1], ab[IHDR_CONTENTS2]);
  1197.  
  1198.       if (iseq != -1)
  1199.     {
  1200.       /* Make sure this packet is in our receive window.  The last
  1201.          packet we have acked is iIlocal_ack.  */
  1202.       if (iIrequest_winsize > 0
  1203.           && CSEQDIFF (iseq, iIlocal_ack) > iIrequest_winsize)
  1204.         {
  1205.           DEBUG_MESSAGE2 (DEBUG_PROTO | DEBUG_ABNORMAL,
  1206.                   "fiprocess_data: Out of order packet %d (ack %d)",
  1207.                   iseq, iIlocal_ack);
  1208.  
  1209.           ++cIbad_order;
  1210.           if (! ficheck_errors (qdaemon))
  1211.         return FALSE;
  1212.  
  1213.           iPrecstart = (iPrecstart + 1) % CRECBUFLEN;
  1214.  
  1215.           continue;
  1216.         }
  1217.     }
  1218.  
  1219.       if (csize > 0)
  1220.     {
  1221.       int cinbuf;
  1222.       char abcksum[CCKSUMLEN];
  1223.       unsigned long ickdata;
  1224.  
  1225.       cinbuf = iPrecend - iPrecstart;
  1226.       if (cinbuf < 0)
  1227.         cinbuf += CRECBUFLEN;
  1228.       if (cinbuf < CHDRLEN + csize + CCKSUMLEN)
  1229.         {
  1230.           if (pcneed != NULL)
  1231.         *pcneed = CHDRLEN + csize + CCKSUMLEN - cinbuf;
  1232.           return TRUE;
  1233.         }
  1234.  
  1235.       if (iPrecend > iPrecstart)
  1236.         {
  1237.           cfirst = csize;
  1238.           zfirst = abPrecbuf + iPrecstart + CHDRLEN;
  1239.         }
  1240.       else
  1241.         {
  1242.           cfirst = CRECBUFLEN - (iPrecstart + CHDRLEN);
  1243.           if (cfirst <= 0)
  1244.         {
  1245.           /* Here cfirst is non-positive, so subtracting from
  1246.              abPrecbuf will actually skip the appropriate number
  1247.              of bytes at the start of abPrecbuf.  */
  1248.           zfirst = abPrecbuf - cfirst;
  1249.           cfirst = csize;
  1250.         }
  1251.           else
  1252.         {
  1253.           if (cfirst >= csize)
  1254.             cfirst = csize;
  1255.           else
  1256.             {
  1257.               zsecond = abPrecbuf;
  1258.               csecond = csize - cfirst;
  1259.             }
  1260.           zfirst = abPrecbuf + iPrecstart + CHDRLEN;
  1261.         }
  1262.         }
  1263.  
  1264.       /* Get the checksum into abcksum.  */
  1265.       for (i = 0, iget = (iPrecstart + CHDRLEN + csize) % CRECBUFLEN;
  1266.            i < CCKSUMLEN;
  1267.            i++, iget = (iget + 1) % CRECBUFLEN)
  1268.         abcksum[i] = abPrecbuf[iget];
  1269.  
  1270.       ickdata = icrc (zfirst, (size_t) cfirst, ICRCINIT);
  1271.       if (csecond > 0)
  1272.         ickdata = icrc (zsecond, (size_t) csecond, ickdata);
  1273.  
  1274.       if (ICKSUM_GET (abcksum) != ickdata)
  1275.         {
  1276.           DEBUG_MESSAGE2 (DEBUG_PROTO | DEBUG_ABNORMAL,
  1277.                   "fiprocess_data: Bad checksum; data %lu, frame %lu",
  1278.                   ickdata, ICKSUM_GET (abcksum));
  1279.  
  1280.           ++cIbad_cksum;
  1281.           if (! ficheck_errors (qdaemon))
  1282.         return FALSE;
  1283.  
  1284.           /* If this sequence number is in our receive window,
  1285.          send a NAK.  iIrecseq is the last sequence number we
  1286.          have succesfully received.  */
  1287.           if (iseq != -1
  1288.           && iseq != iIrecseq
  1289.           && (iIrequest_winsize <= 0
  1290.               || CSEQDIFF (iseq, iIrecseq) <= iIrequest_winsize)
  1291.           && azIrecbuffers[iseq] == NULL)
  1292.         {
  1293.           if (! finak (qdaemon, iseq))
  1294.             return FALSE;
  1295.         }
  1296.  
  1297.           iPrecstart = (iPrecstart + 1) % CRECBUFLEN;
  1298.           continue;
  1299.         }
  1300.     }
  1301.  
  1302.       /* Here we know that this is a valid packet, so we can adjust
  1303.      iPrecstart accordingly.  */
  1304.       if (csize == 0)
  1305.     iPrecstart = (iPrecstart + CHDRLEN) % CRECBUFLEN;
  1306.       else
  1307.     {
  1308.       iPrecstart = ((iPrecstart + CHDRLEN + csize + CCKSUMLEN)
  1309.             % CRECBUFLEN);
  1310.       ++cIreceived_packets;
  1311.     }
  1312.  
  1313.       /* Get the ack from the packet, if appropriate.  iIsendseq is
  1314.      the next sequence number we are going to send, and
  1315.      iIremote_ack is the last sequence number acknowledged by the
  1316.      remote system.  */
  1317.       iack = IHDRWIN_GETSEQ (ab[IHDR_REMOTE]);
  1318.       if (iIremote_winsize > 0
  1319.       && iack != iIsendseq
  1320.       && CSEQDIFF (iack, iIremote_ack) <= iIremote_winsize
  1321.       && CSEQDIFF (iIsendseq, iack) <= iIremote_winsize)
  1322.     {
  1323.       /* Call uwindow_acked each time packet 0 is acked.  */
  1324.       if (iack < iIremote_ack)
  1325.         uwindow_acked (qdaemon, FALSE);
  1326.       iIremote_ack = iack;
  1327.     }
  1328.  
  1329.       if (iseq != -1)
  1330.     {
  1331.       /* If we already sent a NAK for this packet, and we have not
  1332.          seen the previous packet, then forget that we sent a NAK
  1333.          for this and any preceding packets.  This is to handle
  1334.          the following sequence:
  1335.              receive packet 0
  1336.          packets 1 and 2 lost
  1337.          receive packet 3
  1338.          send NAK 1
  1339.          send NAK 2
  1340.          packet 1 lost
  1341.          receive packet 2
  1342.          At this point we want to send NAK 1.  */
  1343.       if (afInaked[iseq]
  1344.           && azIrecbuffers[IPREVSEQ (iseq)] == NULL)
  1345.         {
  1346.           for (i = INEXTSEQ (iIrecseq);
  1347.            i != iseq;
  1348.            i = INEXTSEQ (i))
  1349.         afInaked[i] = FALSE;
  1350.           afInaked[iseq] = FALSE;
  1351.         }
  1352.  
  1353.       /* If we haven't handled all previous packets, we must save
  1354.          off this packet and deal with it later.  */
  1355.       if (iseq != INEXTSEQ (iIrecseq))
  1356.         {
  1357.           if (iseq == iIrecseq
  1358.           || (iIrequest_winsize > 0
  1359.               && CSEQDIFF (iseq, iIrecseq) > iIrequest_winsize))
  1360.         {
  1361.           DEBUG_MESSAGE2 (DEBUG_PROTO | DEBUG_ABNORMAL,
  1362.                   "fiprocess_data: Ignoring out of order packet %d (recseq %d)",
  1363.                   iseq, iIrecseq);
  1364.           continue;
  1365.         }
  1366.           else
  1367.         {
  1368.           DEBUG_MESSAGE2 (DEBUG_PROTO | DEBUG_ABNORMAL,
  1369.                   "fiprocess_data: Saving unexpected packet %d (recseq %d)",
  1370.                   iseq, iIrecseq);
  1371.  
  1372.           if (azIrecbuffers[iseq] == NULL)
  1373.             {
  1374.               azIrecbuffers[iseq] = zbufalc ((size_t) (CHDRLEN
  1375.                                    + csize));
  1376.               memcpy (azIrecbuffers[iseq], ab, CHDRLEN);
  1377.               if (csize > 0)
  1378.             {
  1379.               memcpy (azIrecbuffers[iseq] + CHDRLEN, zfirst,
  1380.                   (size_t) cfirst);
  1381.               if (csecond > 0)
  1382.                 memcpy (azIrecbuffers[iseq] + CHDRLEN + cfirst,
  1383.                     zsecond, (size_t) csecond);
  1384.             }
  1385.             }
  1386.         }
  1387.  
  1388.           /* Send NAK's for each packet between the last one we
  1389.          received and this one, avoiding any packets for which
  1390.          we've already sent NAK's or which we've already
  1391.          received.  */
  1392.           for (i = INEXTSEQ (iIrecseq);
  1393.            i != iseq;
  1394.            i = INEXTSEQ (i))
  1395.         {
  1396.           if (! afInaked[i]
  1397.               && azIrecbuffers[i] == NULL)
  1398.             {
  1399.               if (! finak (qdaemon, i))
  1400.             return FALSE;
  1401.             }
  1402.         }
  1403.  
  1404.           continue;
  1405.         }
  1406.  
  1407.       iIrecseq = iseq;
  1408.     }
  1409.  
  1410.       if (pffound != NULL)
  1411.     *pffound = TRUE;
  1412.  
  1413.       if (! fiprocess_packet (qdaemon, ab, zfirst, cfirst, zsecond, csecond,
  1414.                   pfexit))
  1415.     return FALSE;
  1416.  
  1417.       if (iseq != -1)
  1418.     {
  1419.       int inext;
  1420.  
  1421.       /* If we've already received the next packet(s), process
  1422.          them.  */
  1423.       inext = INEXTSEQ (iIrecseq);
  1424.       while (azIrecbuffers[inext] != NULL)
  1425.         {
  1426.           char *z;
  1427.           int c;
  1428.  
  1429.           z = azIrecbuffers[inext];
  1430.           c = CHDRCON_GETBYTES (z[IHDR_CONTENTS1], z[IHDR_CONTENTS2]);
  1431.           iIrecseq = inext;
  1432.           if (! fiprocess_packet (qdaemon, z, z + CHDRLEN, c,
  1433.                       (char *) NULL, 0, pfexit))
  1434.         return FALSE;
  1435.           ubuffree (azIrecbuffers[inext]);
  1436.           azIrecbuffers[inext] = NULL;
  1437.           inext = INEXTSEQ (inext);
  1438.         }
  1439.     }
  1440.  
  1441.       /* If we have received half of our window size or more since the
  1442.      last ACK, send one now.  Sending an ACK for half the window
  1443.      at a time should significantly cut the acknowledgement
  1444.      traffic when only one side is sending.  We should normally
  1445.      not have to send an ACK if we have data to send, since each
  1446.      packet sent will ACK the most recently received packet.
  1447.      However, it can happen if we receive a burst of short
  1448.      packets, such as a set of command acknowledgements.  */
  1449.       if (iIrequest_winsize > 0
  1450.       && CSEQDIFF (iIrecseq, iIlocal_ack) >= cIack_frequency)
  1451.     {
  1452.       char aback[CHDRLEN];
  1453.  
  1454.       aback[IHDR_INTRO] = IINTRO;
  1455.       aback[IHDR_LOCAL] = IHDRWIN_SET (0, 0);
  1456.       aback[IHDR_REMOTE] = IHDRWIN_SET (iIrecseq, 0);
  1457.       iIlocal_ack = iIrecseq;
  1458.       aback[IHDR_CONTENTS1] = IHDRCON_SET1 (ACK, qdaemon->fcaller, 0);
  1459.       aback[IHDR_CONTENTS2] = IHDRCON_SET2 (ACK, qdaemon->fcaller, 0);
  1460.       aback[IHDR_CHECK] = IHDRCHECK_VAL (aback);
  1461.  
  1462.       DEBUG_MESSAGE1 (DEBUG_PROTO, "fiprocess_data: Sending ACK %d",
  1463.               iIrecseq);
  1464.  
  1465.       if (! (*pfIsend) (qdaemon->qconn, aback, CHDRLEN, TRUE))
  1466.         return FALSE;
  1467.     }
  1468.     }
  1469.  
  1470.   if (pcneed != NULL)
  1471.     *pcneed = CHDRLEN;
  1472.  
  1473.   return TRUE;
  1474. }
  1475.  
  1476. /* Process a single packet.  */
  1477.  
  1478. static boolean
  1479. fiprocess_packet (qdaemon, zhdr, zfirst, cfirst, zsecond, csecond, pfexit)
  1480.      struct sdaemon *qdaemon;
  1481.      const char *zhdr;
  1482.      const char *zfirst;
  1483.      int cfirst;
  1484.      const char *zsecond;
  1485.      int csecond;
  1486.      boolean *pfexit;
  1487. {
  1488.   int ttype;
  1489.  
  1490.   ttype = THDRCON_GETTYPE (zhdr[IHDR_CONTENTS1], zhdr[IHDR_CONTENTS2]);
  1491.   switch (ttype)
  1492.     {
  1493.     case DATA:
  1494.       {
  1495.     int iseq;
  1496.     boolean fret;
  1497.  
  1498.     iseq = IHDRWIN_GETSEQ (zhdr[IHDR_LOCAL]);
  1499.     DEBUG_MESSAGE4 (DEBUG_PROTO,
  1500.             "fiprocess_packet: Got DATA packet %d size %d local %d remote %d",
  1501.             iseq, cfirst + csecond,
  1502.             IHDRWIN_GETCHAN (zhdr[IHDR_REMOTE]),
  1503.             IHDRWIN_GETCHAN (zhdr[IHDR_LOCAL]));
  1504.     fret = fgot_data (qdaemon, zfirst, (size_t) cfirst,
  1505.               zsecond, (size_t) csecond,
  1506.               IHDRWIN_GETCHAN (zhdr[IHDR_REMOTE]),
  1507.               IHDRWIN_GETCHAN (zhdr[IHDR_LOCAL]),
  1508.               iIrecpos,
  1509.               INEXTSEQ (iIremote_ack) == iIsendseq,
  1510.               pfexit);
  1511.     iIrecpos += cfirst + csecond;
  1512.     return fret;
  1513.       }
  1514.  
  1515.     case SYNC:
  1516.       {
  1517.     int ipack, iwin, cchans;
  1518.  
  1519.     /* We accept a SYNC packet to adjust the packet and window
  1520.        sizes at any time.  */
  1521.     if (cfirst + csecond < 3)
  1522.       {
  1523.         ulog (LOG_ERROR, "Bad SYNC packet");
  1524.         return FALSE;
  1525.       }
  1526.     ipack = (zfirst[0] & 0xff) << 8;
  1527.     if (cfirst > 1)
  1528.       ipack |= zfirst[1] & 0xff;
  1529.     else
  1530.       ipack |= zsecond[0];
  1531.     if (cfirst > 2)
  1532.       iwin = zfirst[2];
  1533.     else
  1534.       iwin = zsecond[2 - cfirst];
  1535.  
  1536.     /* The fourth byte in a SYNC packet is the number of channels
  1537.        to use.  This is optional.  Switching the number of
  1538.        channels in the middle of a conversation may cause
  1539.        problems.  */
  1540.     if (cfirst + csecond <= 3)
  1541.       cchans = 0;
  1542.     else
  1543.       {
  1544.         if (cfirst > 3)
  1545.           cchans = zfirst[3];
  1546.         else
  1547.           cchans = zsecond[3 - cfirst];
  1548.         if (cchans > 0 && cchans < 8)
  1549.           qdaemon->cchans = cchans;
  1550.       }
  1551.  
  1552.     DEBUG_MESSAGE3 (DEBUG_PROTO,
  1553.             "fiprocess_packet: Got SYNC packsize %d winsize %d channels %d",
  1554.             ipack, iwin, cchans);
  1555.  
  1556.     if (iIforced_remote_packsize == 0
  1557.         && (iIalc_packsize == 0
  1558.         || ipack <= iIalc_packsize))
  1559.       iIremote_packsize = ipack;
  1560.     iIremote_winsize = iwin;
  1561.  
  1562.     /* We increment a static variable to tell the initialization
  1563.        code that a SYNC was received, and we set *pfexit to TRUE
  1564.        to get out to the initialization code (this will do no harm
  1565.        if we are called from elsewhere).  */
  1566.     ++cIsyncs;
  1567.     *pfexit = TRUE;
  1568.     return TRUE;
  1569.       }
  1570.  
  1571.     case ACK:
  1572.       /* There is nothing to do here, since the ack was already
  1573.      handled in fiprocess_data.  */
  1574.       DEBUG_MESSAGE1 (DEBUG_PROTO,
  1575.               "fiprocess_packet: Got ACK %d",
  1576.               IHDRWIN_GETSEQ (zhdr[IHDR_REMOTE]));
  1577.       return TRUE;
  1578.  
  1579.     case NAK:
  1580.       /* We must resend the requested packet.  */
  1581.       {      
  1582.     int iseq;
  1583.     char *zsend;
  1584.     size_t clen;
  1585.  
  1586.     ++cIremote_rejects;
  1587.     if (! ficheck_errors (qdaemon))
  1588.       return FALSE;
  1589.  
  1590.     iseq = IHDRWIN_GETSEQ (zhdr[IHDR_LOCAL]);
  1591.  
  1592.     /* If the remote side times out while waiting for a packet, it
  1593.        will send a NAK for the next packet it wants to see.  If we
  1594.        have not sent that packet yet, and we have no
  1595.        unacknowledged data, it implies that the remote side has a
  1596.        window full of data to send, which implies that our ACK has
  1597.        been lost.  Therefore, we send an ACK.  */
  1598.     if (iseq == iIsendseq &&
  1599.         INEXTSEQ (iIremote_ack) == iIsendseq)
  1600.       {
  1601.         char aback[CHDRLEN];
  1602.  
  1603.         aback[IHDR_INTRO] = IINTRO;
  1604.         aback[IHDR_LOCAL] = IHDRWIN_SET (0, 0);
  1605.         aback[IHDR_REMOTE] = IHDRWIN_SET (iIrecseq, 0);
  1606.         iIlocal_ack = iIrecseq;
  1607.         aback[IHDR_CONTENTS1] = IHDRCON_SET1 (ACK, qdaemon->fcaller, 0);
  1608.         aback[IHDR_CONTENTS2] = IHDRCON_SET2 (ACK, qdaemon->fcaller, 0);
  1609.         aback[IHDR_CHECK] = IHDRCHECK_VAL (aback);
  1610.  
  1611.         DEBUG_MESSAGE1 (DEBUG_PROTO, "fiprocess_packet: Sending ACK %d",
  1612.                 iIrecseq);
  1613.  
  1614.         return (*pfIsend) (qdaemon->qconn, aback, CHDRLEN, TRUE);
  1615.       }
  1616.     else
  1617.       {
  1618.         if (iseq == iIsendseq
  1619.         || (iIremote_winsize > 0
  1620.             && (CSEQDIFF (iseq, iIremote_ack) > iIremote_winsize
  1621.             || CSEQDIFF (iIsendseq, iseq) > iIremote_winsize)))
  1622.           {
  1623.         DEBUG_MESSAGE2 (DEBUG_PROTO | DEBUG_ABNORMAL,
  1624.                 "fiprocess_packet: Ignoring out of order NAK %d (sendseq %d)",
  1625.                 iseq, iIsendseq);
  1626.         return TRUE;
  1627.           }
  1628.  
  1629.         DEBUG_MESSAGE1 (DEBUG_PROTO | DEBUG_ABNORMAL,
  1630.                 "fiprocess_packet: Got NAK %d; resending packet",
  1631.                 iseq);
  1632.  
  1633.         /* Update the received sequence number.  */
  1634.         zsend = azIsendbuffers[iseq] + CHDROFFSET;
  1635.         if (IHDRWIN_GETSEQ (zsend[IHDR_REMOTE]) != iIrecseq)
  1636.           {
  1637.         int iremote;
  1638.  
  1639.         iremote = IHDRWIN_GETCHAN (zsend[IHDR_REMOTE]);
  1640.         zsend[IHDR_REMOTE] = IHDRWIN_SET (iIrecseq, iremote);
  1641.         zsend[IHDR_CHECK] = IHDRCHECK_VAL (zsend);
  1642.         iIlocal_ack = iIrecseq;
  1643.           }
  1644.           
  1645.         ++cIresent_packets;
  1646.  
  1647.         clen = CHDRCON_GETBYTES (zsend[IHDR_CONTENTS1],
  1648.                      zsend[IHDR_CONTENTS2]);
  1649.  
  1650.         return (*pfIsend) (qdaemon->qconn, zsend,
  1651.                    CHDRLEN + clen + (clen > 0 ? CCKSUMLEN : 0),
  1652.                    TRUE);
  1653.       }
  1654.       }
  1655.  
  1656.     case SPOS:
  1657.       /* Set the file position.  */
  1658.       {
  1659.     char abpos[CCKSUMLEN];
  1660.     const char *zpos;
  1661.  
  1662.     if (cfirst >= CCKSUMLEN)
  1663.       zpos = zfirst;
  1664.     else
  1665.       {
  1666.         memcpy (abpos, zfirst, (size_t) cfirst);
  1667.         memcpy (abpos + cfirst, zsecond, (size_t) (CCKSUMLEN - cfirst));
  1668.         zpos = abpos;
  1669.       }
  1670.     iIrecpos = (long) ICKSUM_GET (zpos);
  1671.     DEBUG_MESSAGE1 (DEBUG_PROTO,
  1672.             "fiprocess_packet: Got SPOS %ld", iIrecpos);
  1673.     return TRUE;
  1674.       }
  1675.  
  1676.     case CLOSE:
  1677.       {
  1678.     boolean fexpected;
  1679.  
  1680.     fexpected = ! fLog_sighup || fIclosing;
  1681.     if (! fexpected)
  1682.       ulog (LOG_ERROR, "Received unexpected CLOSE packet");
  1683.     else
  1684.       DEBUG_MESSAGE0 (DEBUG_PROTO, "fiprocess_packet: Got CLOSE packet");
  1685.  
  1686.     fIclosing = TRUE;
  1687.     *pfexit = TRUE;
  1688.     return fexpected;
  1689.       }
  1690.  
  1691.     default:
  1692.       /* Just ignore unrecognized packet types, for future protocol
  1693.      enhancements.  */
  1694.       DEBUG_MESSAGE1 (DEBUG_PROTO, "fiprocess_packet: Got packet type %d",
  1695.               ttype);
  1696.       return TRUE;
  1697.     }
  1698.   /*NOTREACHED*/
  1699. }
  1700.